<?php

namespace app\common\library;

use fast\Random;
use think\Db;
use think\Hook;
use think\Log;
use app\admin\model\yiliao\Booking;
/**
 * Robot 企业微信
 */
class Robot
{
    //url 密钥
    //protected static $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8a65775b-34f2-4981-be8b-9ee98cedfe46'; //测试
    protected static $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9d76aff0-126f-4ebe-a277-97667efb7a0d';

    /**
     * 预约成功后发送企业微信机器人消息
     * @param $bookId
     * @return void
     */
    public static function send_applyed_message($bookId)
    {
        $book = Booking::where(['id'=>$bookId])->find();
        Log::info('当前发送企业机器人消息时间:'.date('Y-m-d H:i:s'));
        $content = ' ';
        if($book['shopname']=='会议室'){
            $content = $book['doctor'].'会议室有新的预约，请注意查收。\n预约公司名称：'.$book['companyname'].'\n预约日期：'.$book['date'].'\n预约时段：'.$book['times'].'\n参会人数：'.$book['persons'].'\n预约人：'.$book['name'];
        }
        if($book['shopname']=='茶水间'){
            $content = $book['doctor'].'茶水间有新的预约，请注意查收。\n预约公司名称：'.$book['companyname'].'\n预约日期：'.$book['date'].'\n预约时段：'.$book['times'].'\n参会人数：'.$book['persons'].'\n预约人：'.$book['name'];
        }
        if($book['shopname']=='产业园展厅'){
            $content = $book['doctor'].'有新的预约，请注意查收。\n预约公司名称：'.$book['companyname'].'\n预约日期：'.$book['date'].'\n预约时段：'.$book['times'].'\n参会人数：'.$book['persons'].'\n预约人：'.$book['name'];
        }
        $postdata1 ='
        {
            "msgtype": "text",
            "text": {
                "content": "'.$content.'",
                "mentioned_list":["@all"],
            }
        }';
        Log::info('预约text文本:'.$postdata1);
        $status  = qy_applyed_message(self::$url,$postdata1);
        Log::info('结束发送企业机器人消息时间:'.date('Y-m-d H:i:s'));
        return $status;
    }

    /**
     * 取消预约后发送企业微信机器人消息
     * @param $bookId
     * @return void
     */
    public static function remove_applyed_message($bookId)
    {
        $book = Booking::where(['id'=>$bookId])->find();
        Log::info('当前取消预约发送企业机器人消息时间:'.date('Y-m-d H:i:s'));
        $content = ' ';
        if($book['shopname']=='会议室'){
            $content = $book['companyname'].'已取消'.$book['doctor'].'会议室预约\n取消原因:'.$book['reason'];
        }
        if($book['shopname']=='茶水间'){
            $content = $book['companyname'].'已取消'.$book['doctor'].'茶水间预约\n取消原因:'.$book['reason'];
        }
        if($book['shopname']=='产业园展厅'){
            $content = $book['companyname'].'已取消'.$book['doctor'].'预约\n取消原因:'.$book['reason'];
        }
        $postdata1 ='
        {
            "msgtype": "text",
            "text": {
                "content": "'.$content.'",
                "mentioned_list":["@all"],
            }
        }';
        Log::info('取消预约text文本:'.$postdata1);
        $status  = qy_applyed_message(self::$url,$postdata1);
        Log::info('结束取消预约发送发送企业机器人消息时间:'.date('Y-m-d H:i:s'));
        return $status;
    }
}
